package com.example.demo_job.service.impl;


import com.example.demo_job.entity.*;

import com.example.demo_job.mapper.ManagerMapper;
import com.example.demo_job.service.ManagerService;
import com.example.demo_job.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class ManagerServiceimpl implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public List<Seeker> showSeeker() {
        return managerMapper.showSeeker();
    }

    @Override
    public Seeker findBysid(int sid) {
        Seeker seeker=managerMapper.findBysid(sid);
        return seeker;
    }


    @Override
    public Manager findBymname(String mname) {
        return managerMapper.findBymname(mname);
    }

    @Override
    public Manager login(String mname, String mpassword) {
        return managerMapper.login(mname, mpassword);
    }

    @Override
    public List<ComPosition> showPosition() {
        List<Position> positionList=managerMapper.showPosition();
        List<ComPosition> comPositionList=new ArrayList<ComPosition>();
        for(Position position:positionList){
            int cid = position.getCid();
            Company company=managerMapper.findBycid(cid);
            ComPosition comPosition=new ComPosition(position,company);
            comPositionList.add(comPosition);
        }
        return comPositionList;
    }

    @Override
    public Position findBypid(int pid) {
        Position position=managerMapper.findBypid(pid);
        return position;
    }

    @Override
    public void deletePosition(int pid) {
        //删除txt
        Position position = managerMapper.findBypid(pid);
//        File file=new File("C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\PositionTxt\\"+position.getPinfo());
        File file=new File("src\\main\\resources\\static\\PositionTxt\\"+position.getPinfo());
        file.delete();
        managerMapper.deletePosition(pid);
    }

    @Override
    public void deleteRecruit(int pid) {
        managerMapper.deleteRecruit(pid);
    }

    @Override
    public List<Company> showCompany() {
        return managerMapper.showCompany();
    }

    @Override
    public Company findBycid(int cid) {
        return managerMapper.findBycid(cid);
    }

    @Override
    public void deleteCompany(int cid) {
        //删除三个表，company，recruit，position
        //删除公司txt
        Company company = managerMapper.findBycid(cid);
//        File file=new File("C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\CompanyTxt\\"+company.getCinfo());
        File file=new File("src\\main\\resources\\static\\CompanyTxt\\"+company.getCinfo());
        file.delete();
        //删除岗位txt
        List<Position> positionList=managerMapper.findPositionBycid(cid);
        for(Position position:positionList){
            String pinfo = position.getPinfo();
//            File pfile=new File("C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\PositionTxt\\"+pinfo);
            File pfile=new File("src\\main\\resources\\static\\PositionTxt\\"+pinfo);
            pfile.delete();
        }
        //删除招聘表
        managerMapper.deleteRecruitBycid(cid);
        //删除职位表
        managerMapper.deletePositionBycid(cid);
        //删除公司表
        managerMapper.deleteCompany(cid);
    }

    @Override
    public List<RecruitSeekerComPos> showRecruit() {
        List<Recruit> recruitList=managerMapper.showRecruit();
        List<RecruitSeekerComPos> recruitSeekerComPosList=new ArrayList<RecruitSeekerComPos>();
        for(Recruit recruit :recruitList ){
            int sid = recruit.getSid();
            int pid = recruit.getPid();
            Seeker seeker=managerMapper.findBysid(sid);
            Position position=managerMapper.findBypid(pid);
            Company company=managerMapper.findBycid(position.getCid());
            RecruitSeekerComPos recruitSeekerComPos=new RecruitSeekerComPos(recruit,seeker,company,position);
            recruitSeekerComPosList.add(recruitSeekerComPos);
        }
        return recruitSeekerComPosList;
    }

    @Override
    public Recruit findRecruitBysidpid(int sid,int pid) {
        return managerMapper.findRecruitBysidpid(sid,pid);
    }




    @Override
    public void revisePositionAnumber(int pid) {
        managerMapper.revisePositionAnumber(pid);
    }

    @Override
    public void deleteRecruitBysidpid(int sid, int pid) {
        managerMapper.deleteRecruitBysidpid(sid,pid);
    }


    @Override
    public void deleteSeeker(int sid) {
        //删除文件
        Seeker seeker = managerMapper.findBysid(sid);
//        File file=new File("C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\SeekerTxt\\"+seeker.getSinfo());
        File file=new File("src\\main\\resources\\static\\SeekerTxt\\"+seeker.getSinfo());
        file.delete();
        //删除用户表
        managerMapper.deleteSeeker(sid);
        //删除招聘表
        managerMapper.deleteRecruitBysid(sid);
    }
}
